package com.bestcem.xm.ticket.grpc.service;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.common.core.constant.RegTypes;
import com.bestcem.xm.common.core.domain.message.SampleCopyMessage;
import com.bestcem.xm.common.core.domain.web.RpcStatusResult;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.ticket.dao.TicketConfigDao;
import com.bestcem.xm.ticket.dao.TicketDao;
import com.bestcem.xm.ticket.dao.TicketRecordDao;
import com.bestcem.xm.ticket.dao.TicketWechatNotificationDao;
import com.bestcem.xm.ticket.entity.mysql.TicketWechatNotificationDO;
import com.bestcem.xm.ticket.grpc.v1.services.*;
import com.bestcem.xm.ticket.service.TicketMessageReceiverService;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;


/**
 * 文本分析项目相关gRpc接口
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v1.0
 * @date 2021/5/8
 */
@Slf4j
@GrpcService
public class TicketGrpcService extends TicketServiceGrpc.TicketServiceImplBase {

    @Resource
    private TicketDao ticketDao;
    @Resource
    private TicketConfigDao ticketConfigDao;
    @Resource
    private TicketRecordDao ticketRecordDao;
    @Resource
    private TicketWechatNotificationDao ticketWechatNotificationDao;
    //@Resource
    //private TicketMqConsumUtil ticketMqConsumUtil;
    @Resource
    private TicketMessageReceiverService ticketMessageReceiverService;
    @Resource
    private EntityConverter entityConverter;

    /**
     * 获取工单信息
     *
     * @param request          请求
     * @param responseObserver 返回
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/8/30 13:39
     */
    @Override
    public void getTicket(GetTicketRequest request, StreamObserver<GetTicketResponse> responseObserver) {
        GetTicketResponse.Builder builder = GetTicketResponse.newBuilder();

        // 参数校验
        String ticketId = request.getId();
        if (ObjectUtils.isEmpty(ticketId) || !Pattern.matches(RegTypes.OBJECT_ID, ticketId)) {
            builder.setStatus(RpcStatusResult.error("工单id错误"));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        com.bestcem.xm.ticket.entity.mongo.Ticket entity = ticketDao.findById(ticketId);
        if (Objects.nonNull(entity)) {
            builder.setTicket(entityToTicketInfo(entity));
            builder.setStatus(RpcStatusResult.success());
        } else {
            builder.setStatus(RpcStatusResult.error("id对应工单不存在"));
        }

        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }


    /**
     * 获取工单配置信息
     *
     * @param request          请求
     * @param responseObserver 返回
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/8/30 13:39
     */
    @Override
    public void getTicketConfig(GetTicketConfigRequest request, StreamObserver<GetTicketConfigResponse> responseObserver) {
        GetTicketConfigResponse.Builder builder = GetTicketConfigResponse.newBuilder();

        // 参数校验
        String ticketConfigId = request.getId();
        if (ObjectUtils.isEmpty(ticketConfigId) || !Pattern.matches(RegTypes.OBJECT_ID, ticketConfigId)) {
            builder.setStatus(RpcStatusResult.error("工单配置id错误"));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        com.bestcem.xm.ticket.entity.mongo.TicketConfig entity = ticketConfigDao.findById(ticketConfigId);
        if (Objects.nonNull(entity)) {
            builder.setTicketConfig(entityToTicketConfigInfo(entity));
            builder.setStatus(RpcStatusResult.success());
        } else {
            builder.setStatus(RpcStatusResult.error("id对应工单配置不存在"));
        }

        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }


    /**
     * 获取工单对应的答卷信息
     *
     * @param request
     * @param responseObserver
     * @return void
     * @version v1.0
     * @author gy.liu
     * @date 2021/10/22
     */
    @Override
    public void listTicketSeqs(ListTicketSeqsRequest request, StreamObserver<ListTicketSeqsResponse> responseObserver) {
        ListTicketSeqsResponse.Builder builder = ListTicketSeqsResponse.newBuilder();
        if (log.isInfoEnabled()) {
            log.info("[ticket]获取工单对应的答卷信息：param={}", request.getOrgId());
        }
        // 参数校验
        String orgId = request.getOrgId();
        if (ObjectUtils.isEmpty(orgId) || !Pattern.matches(RegTypes.OBJECT_ID, orgId)) {
            builder.setStatus(RpcStatusResult.error("机构id错误"));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        List<String> entity = ticketDao.findByOrgId(orgId);
        if (!CollectionUtils.isEmpty(entity)) {
            builder.addAllSeqs(entity);
            builder.setStatus(RpcStatusResult.success());
        } else {
            builder.setStatus(RpcStatusResult.error("机构id不存在"));
        }

        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }


    @Override
    public void updateTicketMemberId(UpdateTicketMemberIdRequest request, StreamObserver<UpdateTicketMemberIdResponse> responseObserver) {
        UpdateTicketMemberIdResponse.Builder builder = UpdateTicketMemberIdResponse.newBuilder();

        // 参数校验
        List<TicketMemberIdMap> idMapList = request.getIdMapsList();

        if (CollUtil.isEmpty(idMapList)) {
            builder.setStatus(RpcStatusResult.success());
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }
        for (TicketMemberIdMap idMap : idMapList) {
            String ticketId = idMap.getTicketId();
            String memberId = idMap.getMemberId();
            if (ObjectUtils.isEmpty(ticketId) || !Pattern.matches(RegTypes.OBJECT_ID, ticketId) ||
                    ObjectUtils.isEmpty(memberId) || !Pattern.matches(RegTypes.OBJECT_ID, memberId)) {
                log.error("[ticket]: 工单id或联系人id错误,ticketId:{},memberId:{}", ticketId, memberId);
                builder.setStatus(RpcStatusResult.error("[ticket]: 工单id或联系人id错误"));
                responseObserver.onNext(builder.build());
                responseObserver.onCompleted();
                return;
            } else {
                try {
                    ticketDao.updateTicketMemberId(ticketId, memberId);
                } catch (Exception e) {
                    log.error("[ticket]: 工单修改联系人id失败,ticketId:{},memberId:{}", ticketId, memberId, e);
                    builder.setStatus(RpcStatusResult.error("工单修改联系人id失败"));
                    responseObserver.onNext(builder.build());
                    responseObserver.onCompleted();
                    return;
                }
            }
        }
        builder.setStatus(RpcStatusResult.success());
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
        return;
    }

    /**
     * 获取工单处理记录信息
     */
    @Override
    public void listTicketRecords(ListTicketRecordsRequest request, StreamObserver<ListTicketRecordsResponse> responseObserver) {
        ListTicketRecordsResponse.Builder builder = ListTicketRecordsResponse.newBuilder();

        // 参数校验
        String ticketId = request.getId();
        if (ObjectUtils.isEmpty(ticketId) || !Pattern.matches(RegTypes.OBJECT_ID, ticketId)) {
            builder.setStatus(RpcStatusResult.error("工单id错误"));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        List<com.bestcem.xm.ticket.entity.mongo.TicketRecord> ticketRecordList = ticketRecordDao.listByTicketId(ticketId);

        if (CollectionUtils.isEmpty(ticketRecordList)) {
            builder.setStatus(RpcStatusResult.error("工单id无对应工单记录"));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        List<TicketRecord> ticketRecords = new ArrayList<>();
        for (com.bestcem.xm.ticket.entity.mongo.TicketRecord entity : ticketRecordList) {
            TicketRecord info = entityToTicketRecordInfo(entity);
            if (Objects.nonNull(info)) {
                ticketRecords.add(info);
            }
        }

        builder.addAllTicketRecords(ticketRecords);
        builder.setStatus(RpcStatusResult.success());

        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * <pre>
     * 获取通知提醒微信订阅信息
     * </pre>
     */
    @Override
    public void getNotificationWechatSubscription(GetNotificationWechatSubscriptionRequest request, StreamObserver<GetNotificationWechatSubscriptionResponse> responseObserver) {
        GetNotificationWechatSubscriptionResponse.Builder builder = GetNotificationWechatSubscriptionResponse.newBuilder();
        String surveyId = request.getSurveyId();
        String wechatOpenId = request.getWechatOpenId();
        TicketWechatNotificationDO isDo = ticketWechatNotificationDao.findNotificationSubscriber(surveyId, wechatOpenId);
        builder.setSubscribe(Objects.nonNull(isDo));
        builder.setStatus(RpcStatusResult.success());
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }


    // ========================== private model convert ==========================


    private static Ticket entityToTicketInfo(com.bestcem.xm.ticket.entity.mongo.Ticket entity) {
        if (Objects.isNull(entity)) {
            return null;
        }
        Ticket.Builder builder = Ticket.newBuilder();
        if (Objects.nonNull(entity.getId())) {
            builder.setId(entity.getId());
        }
        if (Objects.nonNull(entity.getSysId())) {
            builder.setSysId(entity.getSysId());
        }
        if (Objects.nonNull(entity.getOrgId())) {
            builder.setOrgId(entity.getOrgId());
        }
        if (Objects.nonNull(entity.getTicketNo())) {
            builder.setTicketNo(entity.getTicketNo());
        }
        if (Objects.nonNull(entity.getSurveyId())) {
            builder.setSurveyId(entity.getSurveyId());
        }
        if (Objects.nonNull(entity.getSurveyTitle())) {
            builder.setSurveyTitle(entity.getSurveyTitle());
        }
        if (Objects.nonNull(entity.getFollowRoleId())) {
            builder.setFollowRoleId(entity.getFollowRoleId());
        }
        if (Objects.nonNull(entity.getFollowUserIds())) {
            builder.addAllFollowUserIds(entity.getFollowUserIds());
        }
        if (Objects.nonNull(entity.getAuditUserIds())) {
            builder.addAllAuditUserIds(entity.getAuditUserIds());
        }
        if (Objects.nonNull(entity.getAuditRoleId())) {
            builder.setAuditRoleId(entity.getAuditRoleId());
        }
        if (Objects.nonNull(entity.getCurrentFollowNode())) {
            builder.setCurrentFollowNode(entity.getCurrentFollowNode());
        }
        if (Objects.nonNull(entity.getFollowNodeCount())) {
            builder.setFollowNodeCount(entity.getFollowNodeCount());
        }
        if (Objects.nonNull(entity.getCurrentAuditNode())) {
            builder.setCurrentAuditNode(entity.getCurrentAuditNode());
        }
        if (Objects.nonNull(entity.getAuditNodeCount())) {
            builder.setAuditNodeCount(entity.getAuditNodeCount());
        }
        if (Objects.nonNull(entity.getRegion())) {
            builder.setRegionId(entity.getRegion());
        }
        if (Objects.nonNull(entity.getRegionName())) {
            builder.setRegionName(entity.getRegionName());
        }
        if (Objects.nonNull(entity.getAlertId())) {
            builder.setAlertId(entity.getAlertId());
        }
        if (Objects.nonNull(entity.getAlertRuleIds())) {
            builder.addAllAlertRuleIds(entity.getAlertRuleIds());
        }
        if (Objects.nonNull(entity.getResId())) {
            builder.setRespondentId(entity.getResId());
        }
        if (Objects.nonNull(entity.getContact())) {
            builder.setContact(JSON.toJSONString(entity.getContact()));
        }
        if (Objects.nonNull(entity.getStatus())) {
            builder.setStatusValue(entity.getStatus());
        }
        if (Objects.nonNull(entity.getHasFollowOverdue())) {
            builder.setFollowOverdue(entity.getHasFollowOverdue());
        }
        if (Objects.nonNull(entity.getFollowDeadline())) {
            builder.setFollowDeadline(DateUtil.getTimestamp(entity.getFollowDeadline()));
        }
        if (Objects.nonNull(entity.getHasAuditOverdue())) {
            builder.setAuditOverdue(entity.getHasAuditOverdue());
        }
        if (Objects.nonNull(entity.getAuditDeadline())) {
            builder.setAuditDeadline(DateUtil.getTimestamp(entity.getAuditDeadline()));
        }
        if (Objects.nonNull(entity.getTicketDeadline())) {
            builder.setTicketDeadline(DateUtil.getTimestamp(entity.getTicketDeadline()));
        }
        if (Objects.nonNull(entity.getTicketConfigId())) {
            builder.setTicketConfigId(entity.getTicketConfigId());
        }
        if (Objects.nonNull(entity.getTicketFlow())) {
            builder.setTicketFlow(JSON.toJSONString(entity.getTicketFlow()));
        }
        if (Objects.nonNull(entity.getTicketTemplate())) {
            builder.setTicketTemplate(JSON.toJSONString(entity.getTicketTemplate()));
        }
        if (Objects.nonNull(entity.getTicketRecordIds())) {
            builder.addAllTicketRecordIds(entity.getTicketRecordIds());
        }
        if (Objects.nonNull(entity.getClosedDate())) {
            builder.setClosedTime(DateUtil.getTimestamp(entity.getClosedDate()));
        }
        if (Objects.nonNull(entity.getTicketFlowList())) {
            builder.addAllTicketFlowIds(entity.getTicketFlowList());
        }
        if (Objects.nonNull(entity.getTicketFinishTime())) {
            builder.setTicketFinishTime(DateUtil.getTimestamp(entity.getTicketFinishTime()));
        }
        if (Objects.nonNull(entity.getTicketFailedTime())) {
            builder.setTicketFailedTime(DateUtil.getTimestamp(entity.getTicketFailedTime()));
        }
        if (Objects.nonNull(entity.getCurFollowUserId())) {
            builder.addAllCurrentFollowerIds(entity.getCurFollowUserId());
        }
        if (Objects.nonNull(entity.getAlertRecordId())) {
            builder.setAlertRecordId(entity.getAlertRecordId());
        }
        if (Objects.nonNull(entity.getTemplateDetailFields())) {
            List<String> temp = new ArrayList<>();
            List<com.bestcem.xm.ticket.entity.mongo.Ticket.TemplateDetailField> templateDetailFields = entity.getTemplateDetailFields();
            if (!CollectionUtils.isEmpty(templateDetailFields)) {
                for (com.bestcem.xm.ticket.entity.mongo.Ticket.TemplateDetailField templateDetailField : templateDetailFields) {
                    temp.add(JSON.toJSONString(templateDetailField));
                }
            }
            builder.addAllTemplateDetailFields(temp);
        }
        if (Objects.nonNull(entity.getNeedRecord())) {
            builder.setNeedRecord(entity.getNeedRecord());
        }
        if (Objects.nonNull(entity.getType())) {
            builder.setTypeValue(entity.getType());
        }
        if (Objects.nonNull(entity.getHasCommit())) {
            builder.setCommitRecord(entity.getHasCommit());
        }
        if (Objects.nonNull(entity.getMemberId())) {
            builder.setMemberId(entity.getMemberId());
        }

        return builder.buildPartial();
    }


    private TicketConfig entityToTicketConfigInfo(com.bestcem.xm.ticket.entity.mongo.TicketConfig entity) {
        if (Objects.isNull(entity)) {
            return null;
        }
        TicketConfig.Builder builder = TicketConfig.newBuilder();
        if (Objects.nonNull(entity.getId())) {
            builder.setId(entity.getId());
        }
        if (Objects.nonNull(entity.getSysId())) {
            builder.setSysId(entity.getSysId());
        }
        if (Objects.nonNull(entity.getOrgId())) {
            builder.setOrgId(entity.getOrgId());
        }
        if (Objects.nonNull(entity.getUserId())) {
            builder.setUserId(entity.getUserId());
        }
        if (Objects.nonNull(entity.getSurveyId())) {
            builder.setSurveyId(entity.getSurveyId());
        }
        if (Objects.nonNull(entity.getTicketFlowIds())) {
            builder.addAllTicketFlowIds(entity.getTicketFlowIds());
        }
        if (Objects.nonNull(entity.getAlertId())) {
            builder.setAlertId(entity.getAlertId());
        }
        if (Objects.nonNull(entity.getAlertRuleIds())) {
            builder.addAllAlertRuleIds(entity.getAlertRuleIds());
        }
        if (Objects.nonNull(entity.getCreateTrigger())) {
            builder.setTriggerTypeValue(entity.getCreateTrigger());
        }
        if (Objects.nonNull(entity.getAlertRuleCount())) {
            builder.setAlertRuleCount(entity.getAlertRuleCount());
        }
        if (Objects.nonNull(entity.getOnlyHasContact())) {
            builder.setOnlyHasContact(entity.getOnlyHasContact());
        }
        if (Objects.nonNull(entity.getTicketTemplateId())) {
            builder.setTicketTemplateId(entity.getTicketTemplateId());
        }
        if (Objects.nonNull(entity.getTicketNotifyConfigIds())) {
            builder.addAllTicketNotifyConfigIds(entity.getTicketNotifyConfigIds());
        }
        if (Objects.nonNull(entity.getRuleList())) {
            builder.addAllTicketRules(entity.getRuleList());
        }
        if (Objects.nonNull(entity.getStatusRuleList())) {
            List<String> temp = new ArrayList<>();
            List<JSONObject> statusRuleList = entity.getStatusRuleList();
            if (!CollectionUtils.isEmpty(statusRuleList)) {
                for (JSONObject jsonObject : statusRuleList) {
                    temp.add(jsonObject.toJSONString());
                }
            }
            builder.addAllStatusRules(temp);
        }
        if (Objects.nonNull(entity.getTitle())) {
            builder.setTitle(entity.getTitle());
        }
        if (Objects.nonNull(entity.getState())) {
            builder.setStateValue(entity.getState());
        }
        if (Objects.nonNull(entity.getTicketRuleId())) {
            builder.setTicketRuleId(entity.getTicketRuleId());
        }
        if (Objects.nonNull(entity.getNeedDeal())) {
            builder.setNeedDeal(entity.getNeedDeal());
        }
        if (Objects.nonNull(entity.getNeedRecord())) {
            builder.setNeedRecord(entity.getNeedRecord());
        }
        if (Objects.nonNull(entity.getRemindAdmin())) {
            builder.setNotifyAdmin(entity.getRemindAdmin());
        }
        if (Objects.nonNull(entity.getTemplateDetailFields())) {
            List<String> temp = new ArrayList<>();
            List<com.bestcem.xm.ticket.entity.mongo.Ticket.TemplateDetailField> templateDetailFields = entity.getTemplateDetailFields();
            if (!CollectionUtils.isEmpty(templateDetailFields)) {
                for (com.bestcem.xm.ticket.entity.mongo.Ticket.TemplateDetailField templateDetailField : templateDetailFields) {
                    temp.add(JSON.toJSONString(templateDetailField));
                }
            }
            builder.addAllTemplateDetailFields(temp);
        }
        if (Objects.nonNull(entity.getSmtpId())) {
            builder.setSmtpId(entity.getSmtpId());
        }
        return builder.buildPartial();
    }

    private TicketRecord entityToTicketRecordInfo(com.bestcem.xm.ticket.entity.mongo.TicketRecord entity) {
        if (Objects.isNull(entity)) {
            return null;
        }
        TicketRecord.Builder builder = TicketRecord.newBuilder();
        if (Objects.nonNull(entity.getId())) {
            builder.setId(entity.getId());
        }
        if (Objects.nonNull(entity.getOrgId())) {
            builder.setOrgId(entity.getOrgId());
        }
        if (Objects.nonNull(entity.getUserId())) {
            builder.setUserId(entity.getUserId());
        }
        if (Objects.nonNull(entity.getTitle())) {
            builder.setTitle(entity.getTitle());
        }
        if (Objects.nonNull(entity.getTicketId())) {
            builder.setTicketId(entity.getTicketId());
        }
        if (Objects.nonNull(entity.getRoleId())) {
            builder.setRoleId(entity.getRoleId());
        }
        if (Objects.nonNull(entity.getReason())) {
            builder.setRegionId(entity.getReason());
        }
        if (Objects.nonNull(entity.getRegionName())) {
            builder.setRegionName(entity.getRegionName());
        }
        if (Objects.nonNull(entity.getUserName())) {
            builder.setUserName(entity.getUserName());
        }
        if (Objects.nonNull(entity.getSource())) {
            builder.setSourceValue(entity.getSource());
        }
        if (Objects.nonNull(entity.getEvent())) {
            builder.setEventValue(entity.getEvent());
        }
        if (Objects.nonNull(entity.getSurveyId())) {
            builder.setSurveyId(entity.getSurveyId());
        }
        if (Objects.nonNull(entity.getTicketStatus())) {
            builder.setTicketStatusValue(entity.getTicketStatus());
        }
        if (Objects.nonNull(entity.getReason())) {
            builder.setReason(entity.getReason());
        }
        if (Objects.nonNull(entity.getSample())) {
            builder.setSample(entity.getSample());
        }
        if (Objects.nonNull(entity.getReceiveUserId())) {
            builder.setReceiveUserId(entity.getReceiveUserId());
        }
        if (Objects.nonNull(entity.getReceiveUserName())) {
            builder.setReceiveUserName(entity.getReceiveUserName());
        }
        if (Objects.nonNull(entity.getReceiveRoleId())) {
            builder.setReceiveRoleId(entity.getReceiveRoleId());
        }
        if (Objects.nonNull(entity.getReceiveRoleName())) {
            builder.setReceiveRoleName(entity.getReceiveRoleName());
        }
        if (Objects.nonNull(entity.getReceiveRegionName())) {
            builder.setReceiveRegionName(entity.getReceiveRegionName());
        }
        if (Objects.nonNull(entity.getReceiveRegionId())) {
            builder.setReceiveRegionId(entity.getReceiveRegionId());
        }
        if (Objects.nonNull(entity.getCustomAttr())) {
            builder.setCustomAttr(JSON.toJSONString(entity.getCustomAttr()));
        }
        if (Objects.nonNull(entity.getCreatedId())) {
            builder.setCreateId(entity.getCreatedId());
        }
        if (Objects.nonNull(entity.getUpdatedId())) {
            builder.setUpdateId(entity.getUpdatedId());
        }
        if (Objects.nonNull(entity.getCreatedDate())) {
            builder.setCreateTime(DateUtil.getTimestamp(entity.getCreatedDate()));
        }
        if (Objects.nonNull(entity.getUpdatedDate())) {
            builder.setUpdateTime(DateUtil.getTimestamp(entity.getUpdatedDate()));
        }

        return builder.buildPartial();
    }

    /**
     * <pre>
     * 行业sample
     * </pre>
     */
    @Override
    public void copySample(SampleCopyMessageRequest request,
                           StreamObserver<SampleCopyMessageResponse> responseObserver) {
        SampleCopyMessage sampleCopyMessage = entityConverter.toSampleCopyMessage(request);
        try {
            //Assert.hasText(sampleCopyMessage.getOrgSrcId(), CommonConstant.ORG_SRC_ID_NOT_NULL);
            //Assert.hasText(sampleCopyMessage.getOrgDstId(), CommonConstant.ORG_DST_ID_NOT_NULL);
            //Assert.notEmpty(sampleCopyMessage.getSeqList(), CommonConstant.SEQ_LIST_NOT_NULL);
            Map<String, String> ticketIdMap = ticketMessageReceiverService.copySample(sampleCopyMessage);
            SampleCopyMessageResponse response = SampleCopyMessageResponse.newBuilder().putAllTicketId(ticketIdMap).build();
            responseObserver.onNext(response);
            responseObserver.onCompleted();
        } catch (Exception e) {
            responseObserver.onNext(SampleCopyMessageResponse.newBuilder().setStatus(RpcStatusResult.error(e.getMessage())).build());
            responseObserver.onCompleted();
        }
    }
}
